package com.zto.security.browser;

import com.zto.security.browser.authentication.ZtoAuthenctiationFailureHandler;
import com.zto.security.browser.authentication.ZtoAuthenticationSuccessHandler;
import com.zto.security.core.properties.SecurityProperties;
import com.zto.security.core.validate.code.ValidateCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Created by xingyuchao on 2017/12/17.
 */
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private ZtoAuthenctiationFailureHandler ztoAuthenctiationFailureHandler;

    @Autowired
    private ZtoAuthenticationSuccessHandler ztoAuthenticationSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //用表单登录来进行身份认证，所有的请求都需要身份认证以后才能访问.
//        http.formLogin().loginPage("/signIn.html").loginProcessingUrl("/authentication/form")  //表单登录身份认证,自定义的请求url和自定义处理的action
//        //http.httpBasic()    //httpBasic登录，就是在浏览器弹出一个窗口的那种
//                .and()
//                .authorizeRequests()    //对请求授权
//                .antMatchers("/signIn.html").permitAll()   //对signIn.html请求不需要认证
//                .anyRequest()          //任何请求
//                .authenticated()     //都需要身份认证
//                .and().csrf().disable();  //关闭csrf防护

        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
        validateCodeFilter.setAuthenticationFailureHandler(ztoAuthenctiationFailureHandler);
        validateCodeFilter.setSecurityProperties(securityProperties);
        validateCodeFilter.afterPropertiesSet();

        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .loginPage("/authentication/require").loginProcessingUrl("/authentication/form")  //表单登录身份认证,自定义的请求url和自定义处理的action
                //http.httpBasic()    //httpBasic登录，就是在浏览器弹出一个窗口的那种
                .successHandler(ztoAuthenticationSuccessHandler)   //处理成功的处理器
                .failureHandler(ztoAuthenctiationFailureHandler)   //处理失败的处理器
                .and()
                .authorizeRequests()    //对请求授权
                .antMatchers(
                        "/authentication/require",
                        securityProperties.getBrowser().getSignInPage(),
                        "/code/image").permitAll()
                .anyRequest()          //任何请求
                .authenticated()     //都需要身份认证
                .and().csrf().disable();  //关闭csrf防护
    }
}
